/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div class="item-title">
      <div class="subject">
        <h3>发票管理</h3>
        <h5>商城发票查询及管理</h5>
      </div>
    </div>
    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>查看所有发票记录</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>

    <div class="mDiv">
      <div class="ftitle">
        <h3>发票列表</h3>
        <h5>(共 {{totalCount}} 条记录)</h5>
      </div>
      <div class="fbutton" @click="invoiceExport()">
        <div title="选定行数据导出excel文件,如果不选中行，将导出列表所有数据" class="add">
          <span>
            <i class="fa fa-share"></i>导出发票
          </span>
        </div>
      </div>
      <a href @click.prevent="refresh()" class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
      <h3 v-if="statistic">待开发票:{{statistic.waitCount}} 累计开票:{{statistic.totalCount}}</h3>
      <div class="searchbox" style="margin-top:-30px">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="起始时间"
          end-placeholder="截止时间"
          size="small"
        ></el-date-picker>
        <el-select v-model="status" size="small" clearable placeholder="发票状态">
          <el-option
            v-for="item in invoiceStates"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-button
          class="searchbtn"
          size="small"
          icon="el-icon-search"
          @click="currentPage = 1;getPage()"
        >搜索</el-button>
      </div>
    </div>
    <div class="tablebox">
      <el-table :data="tableData" ref="multipleTable" style="width: 100%;">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="发票编号" prop="invoice_id" width="80"></el-table-column>
        <el-table-column label="订单编号" prop="order_sn" width="150"></el-table-column>
        <el-table-column label="用户名" prop="nickname" width="130" show-overflow-tooltip></el-table-column>
        <el-table-column label="店铺" prop="store_name" width="140" show-overflow-tooltip></el-table-column>
        <el-table-column label="发票类型" prop="invoice_type_desc" width="100"></el-table-column>
        <el-table-column label="开票金额" prop="invoice_money" width="100"></el-table-column>
        <el-table-column label="发票抬头" prop="invoice_title" width="100">
			<template slot-scope="scope">{{scope.row.invoice_title || '--'}}></template>
		</el-table-column>
        <el-table-column label="发票内容" prop="invoice_desc" width="100"></el-table-column>
        <el-table-column label="发票税率" prop="invoice_rate" width="100"></el-table-column>
        <el-table-column label="纳税人识别号" prop="taxpayer" width="120">
			<template slot-scope="scope">{{scope.row.taxpayer || '--'}}></template>
		</el-table-column>
        <el-table-column label="发票状态" prop="status_desc" width="100"></el-table-column>
        <el-table-column label="创建时间" prop="ctime_desc" width="140"></el-table-column>
        <el-table-column label="开票时间" prop="atime_desc" width="140"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
              <el-button
                size="small"
                @click="orderDetail(scope.row.order_sn)"
                icon="el-icon-s-order"
              >查看</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        class="paginationbox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { getInvoicePage, getStatistic } from "@/utils/api";
export default {
  data() {
    return {
      tableData: [],
      currentPage: this.$route.params.currentPage || 1,
      totalCount: 0,
      statistic: null,
      pageSize: 10,
      invoiceStates: [
        {
          value: 0,
          label: "待开"
        },
        {
          value: 1,
          label: "已开"
        }
      ],
      dateRange: null,
      status: null
    };
  },
  created() {
    this.getPage();
    this.getStatistic();
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e;
      this.getPage();
    },
    handleCurrentChange(e) {
      this.currentPage = e;
      this.getPage();
    },
    getPage() {
      var that = this;
      var params = {
        status: this.status,
        page: this.currentPage,
        size: this.pageSize
      };
      if (this.dateRange) {
        params.begin_time = this.dateRange[0] / 1000;
        params.end_time = this.dateRange[1] / 1000 + 86400;
      }
      getInvoicePage(params).then(function(res) {
        that.tableData = res.records;
        that.totalCount = res.total;
      });
    },
    getStatistic() {
      var that = this;
      getStatistic().then(function(res) {
        that.statistic = res;
      });
    },
    refresh() {
      this.getPage();
      this.$message.success("刷新成功！");
    },
    orderDetail(orderSn) {
      this.$router.push({
        name: "order_detail",
        params: {
          sn: orderSn,
          currentPage: this.currentPage
        }
      });
    },
    invoiceExport() {
      //获取选中的记录
      var rows = this.$refs.multipleTable.selection;
      var ids = [];
      rows.forEach(e => {
        ids.push(e.invoice_id);
      });
      //拼接参数
      var paramsStr = "s=1";
      if (ids.length > 0) {
        paramsStr += "&invoice_ids=" + ids.join(",");
      } else {
        if (this.status != null && this.status !== "") {
          paramsStr += "&status=" + this.status;
        }
        if (this.dateRange) {
          paramsStr +=
            "&ctime_begin=" +
            this.dateRange[0] / 1000 +
            "&ctime_end=" +
            (this.dateRange[1] / 1000 + 86400);
        }
      }
      window.location.href = "user/invoice/export?" + paramsStr;
    }
  }
};
</script>

<style scoped>
.searchinput {
  width: auto;
}
</style>
